import detailApi from '../services/detail'

export default {
  namespace: 'detail',

  state: {
    // 商品文描
    productDesc: [],
  },

  effects: {

    // 获取商品文描
    *getGoodsDescData({ payload = {} }, { put, call }) {

      const { data: { body = [], code } } = yield call(detailApi.getGoodsDescData, payload.goodsNo);
      if (code === 200) {

        let productDesc = body.map((item) => {
          if (item.type === 'fmdesc-template-text') {
            item.data.content = item.data.content.replace(/(\<p\>|\<\/p\>)/g, '')
          }
          return item
        })

        yield put({
          type: 'saveProductDescState',
          payload: {
            productDesc
          }
        })

      }
    }

  },

  reducers: {

    // 商品文描
    saveProductDescState(state, { payload = {} }) {

      state.productDesc = payload.productDesc
      return state
    }

  }
}